package hyung.jin.seo;

import hyung.jin.seo.model.NumberBasket;
import hyung.jin.seo.util.Constants;
import hyung.jin.seo.util.Utility;
import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;

public class Main extends TabActivity 
{
	public TabHost mTabHost;
	// 1st	Tab
	private EditText[] myNum;
	private Button generateB;
	private Button statisticsB;
	private Button clearB;
	
	// 2nd Tab
	private EditText[] checkNum;
	private EditText whiteboard;
	private Button checkB;
	private Button cancelB;
	
	private NumberBasket basket;
	private Utility util;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = getTabHost();
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Tab 1").setContent(R.id.tab1));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Tab 2").setContent(R.id.tab2));
        mTabHost.setCurrentTab(0);
        util = Utility.getInstance();
        basket = new NumberBasket();
        
        buildUIForFirstTab();
        buildUIForSecondTab();
    }

	private void buildUIForFirstTab() 
	{
		myNum = new EditText[Constants.NUMBER_BALL+1];
		myNum[0] = (EditText) findViewById(R.id.myNum0);
        myNum[1] = (EditText) findViewById(R.id.myNum1);
        myNum[2] = (EditText) findViewById(R.id.myNum2);
        myNum[3] = (EditText) findViewById(R.id.myNum3);
        myNum[4] = (EditText) findViewById(R.id.myNum4);
        myNum[5] = (EditText) findViewById(R.id.myNum5);
        myNum[6] = (EditText) findViewById(R.id.myNum6);
        generateB = (Button) findViewById(R.id.generateB);
        statisticsB = (Button) findViewById(R.id.statisticsB);
        clearB = (Button) findViewById(R.id.clearB);
        
        generateB.setOnClickListener(new View.OnClickListener() {

         	public void onClick(View v) 
 			{
         		
         		if(isAlreadyWritten())
         		{
         			Toast.makeText(Main.this, "Please try again after clearing number", Toast.LENGTH_LONG).show();
         			return;
         		}
         		basket = util.randomiseNumber(prepareBasket(basket));
 				assignNums();
 			}
 		});
        
        statisticsB.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				if(isAlreadyWritten())
         		{
         			Toast.makeText(Main.this, "Please try again after clearing number", Toast.LENGTH_LONG).show();
         			return;
         		}
				String url = "http://10.7.225.97:2010/jan/match?action=statistics";// + util.putQueryString(nums);
				String result = util.getResponseFromWeb(url);
				String[]nums = util.getQueryString(result);
				for(int i=0; i<Constants.NUMBER_BALL+1; i++)
				{
					myNum[i].setText(util.padding(nums[i]));
					myNum[i].setTextColor(Color.BLUE);
				}
			}
		});
        
         clearB.setOnClickListener(new View.OnClickListener() {
 			
 			public void onClick(View v) 
 			{
 				clearNums(myNum);
 			}

 		});
	}
	
	private void buildUIForSecondTab() 
	{
		checkNum = new EditText[Constants.NUMBER_BALL+1];
		checkNum[0] = (EditText) findViewById(R.id.checkNum0);
		checkNum[1] = (EditText) findViewById(R.id.checkNum1);
		checkNum[2] = (EditText) findViewById(R.id.checkNum2);
		checkNum[3] = (EditText) findViewById(R.id.checkNum3);
		checkNum[4] = (EditText) findViewById(R.id.checkNum4);
		checkNum[5] = (EditText) findViewById(R.id.checkNum5);
		checkNum[6] = (EditText) findViewById(R.id.checkNum6);
		whiteboard = (EditText) findViewById(R.id.temp);
		checkB = (Button) findViewById(R.id.checkB);
		cancelB = (Button) findViewById(R.id.cancelB);
		
		
		checkB.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				String[] nums = gatherNums();
//				String url = "http://10.0.0.17:8080/jan/match/Me?nums=" + util.putQueryString(nums);
				String url = "http://10.0.0.17:8080/jan/match?action=result";// + util.putQueryString(nums);
				
				whiteboard.setText(util.getResponseFromWeb(url));
			}
		});
		cancelB.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				clearNums(checkNum);
				whiteboard.setText("");
			}
		});
		
	}

	private String[] gatherNums() 
	{
		String[] nums = new String[Constants.NUMBER_BALL+1];
		for(int i=0; i<Constants.NUMBER_BALL+1; i++)
		{
			nums[i] = checkNum[i].getText().toString();
		}
		return nums;
	}
	
	private void assignNums() 
    {
    	if(myNum!=null)
    	{
    		String[] returnedNum = basket.getNumbers();
    		boolean[] isSelected = basket.getSelected();
    		for(int i=0; i<Constants.NUMBER_BALL+1; i++)
    		{
    			myNum[i].setText(util.padding(returnedNum[i]));
    			if(isSelected[i])
    			{
    				// differenciate color in EditText
    				myNum[i].setTextColor(Color.RED);
    			}
    		}
    	}
	}
    
	private void clearNums(EditText[] nums) 
	{
		if(nums!=null)
		{
			for(int i=0; i<Constants.NUMBER_BALL+1; i++)
			{
				nums[i].setText(Constants.EMPTY_NUMBER);
				nums[i].setTextColor(Color.BLACK);
			}
		}
	}
	
	private boolean isAlreadyWritten() 
	{
		boolean isWritten = !(myNum[0].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER))
							&& !(myNum[1].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER))
							&& !(myNum[2].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER))
							&& !(myNum[3].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER))
							&& !(myNum[4].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER))
							&& !(myNum[5].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER))
							&& !(myNum[6].getText().toString().equalsIgnoreCase(Constants.EMPTY_NUMBER));
		if(isWritten)
		{
			// all already written so just guide cleaning up and try again
			return true;
		}else{
			// prepare sending basket with already written numbers
			return false;
		}
	}
	
	private NumberBasket prepareBasket(NumberBasket basket) 
	{
		String[] textValues = new String[Constants.NUMBER_BALL+1];
		for(int i=0; i<Constants.NUMBER_BALL+1; i++)
		{
			textValues[i] = myNum[i].getText().toString();
			myNum[i].setTextColor(Color.BLACK);
		}
		boolean[] selectedValues = new boolean[Constants.NUMBER_BALL+1];
		basket.setNumbers(textValues);
		basket.setSelected(selectedValues);
		return basket;
	}

}